package com.guagua.chat.topic;
/**
 * 北京八度互联科技有限公司版权所有
 * Copyright (C) Badu Corporation. All Rights Reserved
 */
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpStatus;
import org.apache.log4j.Logger;

/**
 * 同步发布说说到空间
 * @author liyi
 *
 */
public class SynchroToZone extends Thread {
	
	private static Logger log = Logger.getLogger(SynchroToZone.class);

	private long userId;//用户ID
	
	private String content;//发布内容
	
	private HttpServletRequest request;// 为了获取cookies
	
	public SynchroToZone(long userId,String content,HttpServletRequest request){
		this.userId = userId;
		this.content = content;
		this.request = request;
	}
	
	@Override
	public void run() {
		try{
			HttpClient httpClient = new HttpClient();
			
			httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");
			
			PostMethod postMethod = new PostMethod("http://zone.17guagua.com/"+this.userId+"/weibo");
			
			NameValuePair[]  nameValuePairs = {
	                new NameValuePair("content", content)
	        };

			
			postMethod.setRequestBody(nameValuePairs);
			
			Cookie[] cookies = request.getCookies();
			
		
			
			if(cookies !=null && cookies.length>0){
              
				String cookie=cookies[0].getValue();
                for (int i = 1; i < cookies.length; i++) {
                	cookie += "; " + cookies[i].getName() + "=" + cookies[i].getValue();
                }
                postMethod.setRequestHeader("Cookie", cookie);//cookies
                log.debug("cookie = "+cookie);
			}
			
			int state = httpClient.executeMethod(postMethod);
			log.debug("state[1] : "+state);
			if(state == HttpStatus.SC_OK){
				String info = new String(postMethod.getResponseBodyAsString());// {"error":"","message":"发布微博成功","state":0}
				
			}
           
			
			
		}catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}

}
